﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using SVC.client;
namespace UI.Forms
{
    public partial class Select_Product : DevExpress.XtraEditors.XtraForm
    {
        public delegate void DoAction(List<Product_Select> cs);//如果有参数的话，定义参数 
        public DoAction FatherRefresh;
        BindingList<Product_Select> casl;
        private string kcid = ""; //仓库ID
        /// <summary>
        /// 仓库ID
        /// </summary>
        /// <param name="kcid"></param>
        public Select_Product(string _kcid)
        {
            InitializeComponent();
            kcid = _kcid;
        }

        private void Select_BC_Load(object sender, EventArgs e)
        {
            bind();
        }

        private void bind()
        {

            BindingList<SVC.client.View_Goods_Lite> goods = my.bw.Product_Get(kcid, my.ACID);
            casl = new BindingList<Product_Select>();
            if (!string.IsNullOrEmpty(kcid))//销售出库单时只查询出有库存的产品，没有库存的产品不显示出来
            {
                foreach (SVC.client.View_Goods_Lite item in goods)
                {
                    if (item.onhandcount > 0) 
                    {
                        Product_Select ps = new Product_Select();
                        ps.Selected = false; //默认未选中
                        ps.GoodsCode = item.GoodsCode;
                        ps.ClassName = item.ClassName;
                        ps.GoodsName = item.GoodsName;
                        ps.Unit = item.Unit;
                        ps.outprice = item.outprice;
                        ps.LowPrice = item.LowPrice;
                        ps.onhandcount = item.onhandcount;
                        ps.inprice = item.inprice.Value; //进价
                        casl.Add(ps);
                    }
                }

            }
            else
            {
                foreach (SVC.client.View_Goods_Lite item in goods)
                {
                    Product_Select ps = new Product_Select();
                    ps.Selected = false; //默认未选中
                    ps.GoodsCode = item.GoodsCode;
                    ps.ClassName = item.ClassName;
                    ps.GoodsName = item.GoodsName;
                    ps.Unit = item.Unit;
                    ps.outprice = item.outprice;
                    ps.LowPrice = item.LowPrice;
                    ps.onhandcount = item.onhandcount;
                    ps.inprice = item.inprice.Value; //进价
                    casl.Add(ps);

                }
            }
            gridControl1.DataSource = casl;
            

        }

        private void btn_sx_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            bind();
        }

        private void btn_OK_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            foreach (Product_Select item in casl)
            {
                int[] rowHandles = gridView1.GetSelectedRows();
                for (int i = 0; i < rowHandles.Length; i++)
                {
                    if (gridView1.GetRowCellValue(rowHandles[i], gridColumn2).ToString() == item.GoodsCode)
                    {
                        item.Selected = true;
                    }
                }

            }
            FatherRefresh(casl.Where(p => p.Selected).ToList());
            this.Close();
        }


    }
}